package com.imast.android.bys.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import com.imast.android.bys.R;
import com.imast.android.bys.io.model.Producto;
import com.imast.android.bys.task.SoapObjectTaskFragment.TaskCallbacks;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ProductoFragment extends Fragment implements TaskCallbacks {
	
	public static final String ARG_TITLE = "title";
	public static final String ARG_INDEX = "index";
	
	private ProductoHelper helper;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_producto, container, false);
		
		helper = new ProductoHelper(this, getActivity(), rootView);
		
		return rootView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		helper.onActivityCreated();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		helper.hidePDialog();
	}

	@Override
	public void onPreExecute() {
		helper.showDialog();
	}

	@Override
	public void onProgressUpdate(int percent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCancelled() {
		helper.hidePDialog();
	}

	@Override
	public void onPostExecute(SoapObject soapObject) {
		
		List<Producto> lstProducto = new ArrayList<Producto>();
		
		for(int i = 0; i < soapObject.getPropertyCount(); i++){
			
			SoapObject so = (SoapObject) soapObject.getProperty(i);
			
			int idProducto = 0;
			String titulo = "";
			String descripcion = "";
			String thumbnail = "";
			double precio = 0.0;
			
			
			for (int j = 0; j < so.getPropertyCount(); j++){
				idProducto = Integer.valueOf(so.getPropertyAsString("CodProducto"));
				titulo = so.getPropertyAsString(("Descripcion"));
				descripcion = so.getPropertyAsString(("Descripcion"));
				thumbnail = so.getPropertyAsString(("ImagenURL"));
				precio = Double.valueOf(so.getPropertyAsString("Precio"));
			}
			
			lstProducto.add(new Producto(idProducto, titulo, descripcion, thumbnail, precio));
		}
		
		helper.setAdapterChange(lstProducto);
		helper.hidePDialog();
	}

}
